<template>
  <div>
    <div class="add-admin">
      <el-form ref="addAdminFormRef" label-width="80px" :model="form">
        <el-form-item label="所属角色" prop="roleid">
          <el-select
            v-model="form.roleid"
            placeholder="请选择所属角色"
            clearable
          >
            <el-option label="请选择" disabled value="-1"></el-option>
            <el-option
              v-for="role in roleList"
              :key="role.id"
              :label="role.rolename"
              :value="role.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="commit">添加</el-button>
          <el-button @click="$router.push('/user')" type="info">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { getUserApi, addUserApi } from "@/api/user";
import { getRoleListApi } from "@/api/roles";
export default {
  data() {
    return {
      roleList: [],
      // 管理员添加表单数据
      form: {
        roleid: null,
        username: "",
        password: "",
        status: 1,
      },
    };
  },
  mounted() {
    this.getRoleList();
    this.getList()
  },
  methods: {
    async commit() {
      let res = await addUserApi(this.form);
      if (res.code == 200) {
        alert(res.msg);
        this.$Bus.$emit("updata");
        this.form = {
          roleid: null,
          username: "",
          password: "",
          status: 1,
        };
      } else {
        alert(res.msg);
      }
    },
    async getRoleList() {
      let res = await getUserApi(this.$route.query.uid);
      this.form = res.list;
      console.log(res);
    },
    async getList() {
      let res = await getRoleListApi();
      this.roleList = res.list;
      console.log(res);
    },
  },
};
</script>
<style lang="less" scoped>
.add-admin {
  .el-form {
    width: 400px;
  }
}
</style>
